<template>
  <el-dialog @open="openDialog" :title="isAdd?'新增':'编辑'" :visible="dialogFormVisible" @close="closeDialog">
    <el-form :model="form" label-width="80px" :rules="rules" >
      <el-form-item label="头像" prop="avatar">
        <el-upload
          class="avatar-uploader"
          :action="host+'/guard/upload?module=avatar'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.avatar" :src="host+form.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="用户名" prop="account" >
        <el-input v-model="form.account"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="real_name" >
        <el-input v-model="form.real_name"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio v-model="form.gender" label="1">男</el-radio>
        <el-radio v-model="form.gender" label="2">女</el-radio>
      </el-form-item>

      <el-form-item label="电话" prop="phone" >
        <el-input  v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input  type="number" v-model.number="form.age"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address" >
        <el-input   v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="beizhu">
        <el-input :rows="3" type="textarea" v-model="form.beizhu"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="2"
          active-text="启用"
          inactive-text="禁用">
        </el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

import {myPost} from "@/utils/myUtils";

const urlAdd = '/guard/user/add'

export default {
  name: "AddDialog",
  props:{
    dialogFormVisible:{
      type:Boolean,
      default:false
    },
    record:{
      type:Object,
      default:{}
    }
  },

  data(){
    let checkPhone = (rule, value, callback)=>{
      const reg =/^[1][3-9][0-9]{9}$/;
      if(value==''||value==undefined||value==null){
        callback();
      }else {
        if ((!reg.test(value)) && value != '') {
          callback(new Error('请输入正确的电话号码'));
        } else {
          callback();
        }
      }
    }
    return {
      form:{
        id:0,
        account: '',
        real_name: '',
        phone: '',
        age: 20,
        gender: '1',
        beizhu: '',
        avatar: '',
        address: '',
        status:true,
      },
      rules: {
        account: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        real_name: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入'},
          { type: 'number', message: '年龄必须为数字值'}
        ],
        phone:[
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        /* email:[
            { required: true, message: '请输入邮箱格式', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ], */
      },
      host:process.env.VUE_APP_HOST
    }
  },
  methods:{

    handleAvatarSuccess(res, file) {
      if(res.status){
        this.form.avatar = res.data.records.filePath
      }else{
        this.$message.error('上传失败!')
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },

    onSubmit(){
      myPost(urlAdd,this.form).then(res=>{
        if(res){
          this.$message.success(res.msg)
          this.$emit('success',false)
        }
      })
    },
    cancel(){
      this.$emit('cancelDialog',false)
      Object.assign(this.$data.form, this.$options.data().form)
    },
    closeDialog(){
      this.cancel()
    },
    openDialog(){
      Object.assign(this.$data.form, this.$options.data().form)
      //record有数据说明是编辑
      if(Object.keys(this.record).length>0){
        this.form.id = this.record.id
        this.form.real_name = this.record.real_name
        this.form.account = this.record.account
        this.form.phone = this.record.phone
        this.form.age = this.record.age
        this.form.address = this.record.address
        this.form.beizhu = this.record.beizhu
      }
    },
  },

  computed: {
    isAdd: function() {
      return Object.keys(this.record).length<=0;
    }
  },

}
</script>

<style scoped>

</style>
